﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vw08_equest.Areas.Administrator.Models;
using System.Transactions;

namespace vw08_equest.Areas.Administrator.Controllers
{
    public class AdminQuestionController : Controller
    {
        //
        // GET: /Administrator/AdminQuestion/

        public ActionResult Index(string message = "", string level = "")
        {
            if (level == "Success")
            {
                ViewBag.Success = message;
            }
            if (level == "Warning")
            {
                ViewBag.Warning = message;
            }
            if (level == "Error")
            {
                ViewBag.Error = message;
            }
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                List<Question> model = new List<Question>();
                model = db.Questions.Where(m => m.IsDelete == false).OrderBy(m => m.Question1).ToList();
                return View(model);
            }
        }

        public ActionResult CreateQuestion()
        {
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                QuestionAnswerCustom model = new QuestionAnswerCustom();
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult CreateQuestion(QuestionAnswerCustom model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.Warning = "Dữ liệu câu hỏi không đúng!";
                    return View(model);
                }
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    db.InsertQuestion(model.Question, model.Level, model.TrueAnswer, model.FalseAnswer1, model.FalseAnswer2, model.FalseAnswer3);
                    return RedirectToAction("Index", new { message = "Thêm câu hỏi thành công!", level = "Success" });
                }
            }
            catch (Exception)
            {
                ViewBag.Error = "Lỗi tạo câu hỏi!";
                return View(model);
            }
        }

        public ActionResult EditQuestion(int id)
        {
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                List<QuestionSelectResult> model = new List<QuestionSelectResult>();
                model = db.QuestionSelect(id).ToList();
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult EditQuestion(FormCollection form)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    try
                    {
                        int QuestionID = Convert.ToInt32(form["QuestionID"]);
                        string Question1 = form["Question"];
                        int Level = Convert.ToInt32(form["Level"]);
                        string trueAnswer = form["TrueAnswer"];
                        string falseAnswer = form["FalseAnswer"];
                        string[] array = falseAnswer.Split(',');
                        Question myQuestion = db.Questions.Where(m=>m.QuestionID == QuestionID).SingleOrDefault();
                        myQuestion.Question1 = Question1;
                        myQuestion.QuestionLevel = Level;
                        db.SubmitChanges();

                        List<int> answerIDList = new List<int>();
                        answerIDList = (from i in db.Question_Answers
                                        where i.QuestionID == QuestionID
                                        select i.AnswerID).ToList();
                        int index = 0;
                        foreach(int j in answerIDList)
                        {
                            Answer myAnswer = db.Answers.Where(m=>m.AnswerID == j).SingleOrDefault();
                            if(myAnswer.IsTrue == true)
                            {
                                myAnswer.Answer1 = trueAnswer;
                                db.SubmitChanges();
                            }
                            else
                            {
                                myAnswer.Answer1 = array[index];
                                db.SubmitChanges();
                                index++;
                            }
                        }
                        ViewBag.Success = "Cập nhật câu hỏi thành công!";
                        List<QuestionSelectResult> model = new List<QuestionSelectResult>();
                        model = db.QuestionSelect(QuestionID).ToList();
                        trans.Complete();
                        return View(model);
                    }
                    catch (Exception ex) 
                    {
                        ViewBag.Error = "Lỗi cập nhật câu hỏi!";
                        int QuestionID = Convert.ToInt32(form["QuestionID"]);
                        List<QuestionSelectResult> model = new List<QuestionSelectResult>();
                        model = db.QuestionSelect(QuestionID).ToList();
                        return View(model);
                    }
                }
            }
        }

        [HttpPost]
        public ActionResult DeleteQuestion(FormCollection form)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    try
                    {
                        int QuestionID = Convert.ToInt32(form["id"]);
                        Question myQuestion = db.Questions.Where(m => m.QuestionID == QuestionID).SingleOrDefault();
                        myQuestion.IsDelete = true;
                        db.SubmitChanges();
                        List<int> answerIDList = new List<int>();
                        answerIDList = (from i in db.Question_Answers
                                        where i.QuestionID == QuestionID
                                        select i.AnswerID).ToList();
                        foreach (int j in answerIDList)
                        {
                            Answer myAnswer = db.Answers.Where(m => m.AnswerID == j).SingleOrDefault();
                            myAnswer.IsDelete = true;
                            db.SubmitChanges();
                            Question_Answer myQA = db.Question_Answers.Where(m => (m.QuestionID == QuestionID) && (m.AnswerID == j)).SingleOrDefault();
                            myQA.IsDelete = true;
                            db.SubmitChanges();
                        }
                        trans.Complete();
                        return RedirectToAction("Index", new { message = "Xóa câu hỏi thành công!", level = "Success" });
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("Index", new { message = "Lỗi xóa câu hỏi!", level = "Error" });
                    }
                }
            }
        }
    }
}
